/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package forensicsGUI;

import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @author marc
 */
import java.util.TimerTask;
import java.io.*;

import java.util.Vector;
import javax.swing.JCheckBox;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public final class IntermittentSave extends TimerTask {

    private ProgramState programState = null;
    private Vector<String> imageList = null;
    private JTextField investigatorName = null;
    private JTextField caseNumber = null;
    private JTextArea caseDetails = null;
    private JTextField exeDir = null;
    private JTextField exec = null;
    private JTextField maxMem = null;
    private JTextField maxThreads = null;
    private JTextField outputDir = null;
    private JCheckBox quickMode;
    private JCheckBox partialHeaders;
    private JCheckBox auditOnly;
    private boolean parallel = true;

    /**
     * Set the parallel boolean value.
     * @param parallel
     */
    public void setParallel(boolean parallel) {
        this.parallel = parallel;
    }

    /**
     * Set the program state monitor.
     * @param state
     */
    public void setStateManager(ProgramState state) {
        programState = state;
    }

    /**
     * Set the image list.
     * @param imageList
     */
    public void setImageList(Vector<String> imageList) {
        this.imageList = imageList;

    }

    public void setCaseField(JTextField caseField) {
        caseNumber = caseField;
    }

    public void setInvestigatorField(JTextField investigatorName) {
        this.investigatorName = investigatorName;
    }

    public void setCaseDetailsField(JTextArea caseDetails) {
        this.caseDetails = caseDetails;
    }

    public JTextField getInstallDir() {
        return exeDir;
    }

    public void setInstallDir(JTextField dir) {
        exeDir = dir;
    }

    public JTextField getExecutable() {
        return exec;
    }

    public void setExecutable(JTextField exec) {
        this.exec = exec;
    }

    public JTextField getMaxMem() {
        return maxMem;
    }

    public void setMaxMem(JTextField mem) {
        maxMem = mem;
    }

    public JTextField getMaxThreads() {
        return maxThreads;
    }

    public void setMaxThreads(JTextField threads) {
        maxThreads = threads;
    }

    public JTextField getOutputDir() {
        return outputDir;
    }

    public void setOutputDir(JTextField directory) {
        outputDir = directory;
    }

    /**
     * Implements TimerTask's abstract run method.
     */
    public void setQuickModeField(JCheckBox quickMode) {
        this.quickMode = quickMode;
    }

    public void setPartialHeadersField(JCheckBox partialHeaders) {
        this.partialHeaders = partialHeaders;
    }

    public void setAuditOnlyField(JCheckBox auditOnly) {
        this.auditOnly = auditOnly;
    }

    public int doSave()
            throws FileNotFoundException, IOException {

        if (programState == null) {
            // this is an error
            return 1;
        }

        programState.setCaseDetails(caseDetails.getText());
        programState.setCaseNumber(caseNumber.getText());
        programState.setImageList(imageList);
        programState.setInvestigatorName(investigatorName.getText());
        programState.setExecutable(exec.getText());
        programState.setInstallDir(exeDir.getText());
        programState.setMaxMem(maxMem.getText());
        programState.setAuditOnly(auditOnly.isSelected());
        programState.setPartialHeaders(partialHeaders.isSelected());
        programState.setQuickMode(quickMode.isSelected());
        programState.setOutputDir(outputDir.getText());
        programState.setParallel(parallel);
        FileOutputStream fout = new FileOutputStream("foremostgui.dat");
        ObjectOutputStream oos = new ObjectOutputStream(fout);
        oos.writeObject(programState);
        oos.close();
        System.out.println("saving");


        return 1;
    }

    public void run() {
        try {
            //toy implementation
            doSave();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(IntermittentSave.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(IntermittentSave.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
